//
//  StrategyInfoViewController.m
//  Community
//
//  Created by YaSha_Tom on 2018/12/21.
//  Copyright © 2018年 YaSha-Tom. All rights reserved.
//

#import "StrategyInfoViewController.h"
#import "SceneryOneTableViewCell.h"
#import "SceneryTwoTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "FoundModel.h"
#import "DataManager.h"
#import <AFNetworking.h>
#import <QMUIKit.h>

@interface StrategyInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *dataSource;

@end

@implementation StrategyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.topic;
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
    [self doNetworking];
}
- (void)initUI {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    }else{
        return 30;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }else {
        UIView *view = [[UIView alloc]init];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, 345, 20)];
        FoundModel *model = self.dataSource[section];
        lable.text = [NSString stringWithFormat:@"%ld.  %@",(long)section,model.topic] ;
        [view addSubview:lable];
        return view;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0){
        SceneryOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sceneryOneCell"];
        if(cell == nil){
            cell = [[SceneryOneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sceneryOneCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell 的点击效果
        if (self.model.descrip.length > 0) {
            cell.contentLabel.text = self.model.descrip;
        }else if (self.model.travel_tip.length > 0){
            cell.contentLabel.text = self.model.travel_tip;
        }else{
            cell.contentLabel.text = @"暂无介绍";
        }
//        cell.contentLabel.text = self.model.descrip;
        return cell;
    }else {
        SceneryTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if(cell == nil){
            cell = [[SceneryTwoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;//取消cell 的点击效果
        FoundModel *model = self.dataSource[indexPath.section];
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:model.photo[@"photo_url"]] placeholderImage:[UIImage imageNamed:@" "]];
        cell.contentLabel.text  =model.introduce;
        return cell;
    }
}

- (void)doNetworking {
    // 1.创建请求管理对象
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSString *url =[NSString stringWithFormat:@"http://q.chanyouji.com/api/v2/activity_collections/%@.json",self.model.id];
    NSLog(@"--------%@",url);
    [manger GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功%@",responseObject);
        if ([responseObject[@"status"]  isEqual: @200]) {
            self.dataSource = [DataManager getStrategyInfoData:responseObject];
            [self.tableView reloadData];
            
        }else{
            [QMUITips showSucceed:@"请求数据失败" inView:self.view hideAfterDelay:1];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
